<template>
  <div class="app-container">
    <el-card>
      <el-form :model="searchForm" inline size="mini">
        <el-form-item label="标">
          <el-input v-model="searchForm.title" placeholder="请输入标题" clearable />
        </el-form-item>
        <el-form-item label="作者ID">
          <el-input v-model="searchForm.memberId" type="number" placeholder="请输入作者ID" clearable />
        </el-form-item>

        <el-form-item label="类型">
          <el-select v-model="searchForm.type" placeholder="请选择类型" clearable>
            <el-option label="文章" value="1" />
            <el-option label="短视频" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.verifyStatus" placeholder="请选择状态" clearable>
            <el-option label="待审核" value="0" />
            <el-option label="审核通过" value="1" />
            <el-option label="未通过" value="2"></el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="查询时间：" prop="beginTime">
          <el-date-picker style="width: 200px" v-model="searchForm.beginTime" align="right" type="date"
            placeholder="选择开始日期" :picker-options="$pickerDateOptions" value-format="yyyy-MM-dd"
            format="yyyy 年 MM 月 dd 日">
          </el-date-picker>
        </el-form-item>
        <el-form-item>-</el-form-item>
        <el-form-item prop="endTime">
          <el-date-picker style="width: 200px" v-model="searchForm.endTime" align="right" type="date"
            placeholder="选择结束日期" :picker-options="$pickerDateOptions" value-format="yyyy-MM-dd"
            format="yyyy 年 MM 月 dd 日">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button  icon="el-icon-refresh" @click="reset()">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" size="mini" icon="el-icon-delete" @click="batchDelete()">批量删除</el-button>

      <el-table size="mini" v-loading="listLoading" :data="tableData.list" @selection-change="handleSelectionChange"
        border style="margin-top: 20px">
        <el-table-column align="center" type="selection" width="55"></el-table-column>
        <el-table-column align="center" label="序号" type="index"></el-table-column>

        <el-table-column align="center" label="评论内容" prop="comment" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column align="center" label="类别">
          <template slot-scope="scope">
            <el-tag size="mini" type="primary">{{
              scope.row.type | typeFilter
              }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="文章/短视频" prop="title" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column align="center" label="作者昵称" prop="memberNickname">
        </el-table-column>
        <el-table-column align="center" label="作者ID" prop="memberId">
        </el-table-column>
        <el-table-column align="center" label="回复时间" prop="createTime">
        </el-table-column>
        <el-table-column align="center" label="审核状态" prop="verifyStatus">
          <template slot-scope="scope">
            <el-tag type="info" size="mini" v-if="scope.row.verifyStatus == 0">待审核</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.verifyStatus == 1">审核通过</el-tag>
            <el-tag type="warning" size="mini" v-if="scope.row.verifyStatus == 2">未通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="审核时间" prop="verifyTime">
          <template slot-scope="scope">
            {{ scope.row.verifyTime ? scope.row.verifyTime : "-" }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="mini" v-if="scope.row.verifyStatus == 0"
              @click="editArticleStatus(scope.row)">审核</el-button>
            <el-button type="text" size="mini" @click="deleteComment(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="pageRight" background layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]" :page-size="searchForm.pageSize" :total="tableData.total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </el-card>
    <edit ref="edit" @getList="getList"></edit>
  </div>
</template>
<script>
import { getArticleCommentList, deleteCommentAndReply } from "@/api/article";
import edit from "./components/edit.vue";
export default {
  components: { edit },
  name: "index",
  filters: {
    typeFilter(type) {
      const mapObj = {
        1: "文章",
        4: "问答",
        3: "短视频"
      };
      return mapObj[type];
    }
  },
  data() {
    return {
      tableData: {
        total: 0,
        list: []
      },
      listLoading: false,
      searchForm: {
        title: "",
        memberId: "",
        verifyStatus: "",
        typeList: "1,3",
        pageNum: 1,
        pageSize: 10,
        type: ''
      },
      selectList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSelectionChange(v) {
      this.selectList = v.map(item => {
        return {
          commentId: item.commentId,
          replayId: item.replayId
        };
      });
    },
    editArticleStatus(v) {
      this.$refs.edit.opendialog(v);
    },
    deleteComment(v) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let params = {
          commentId: v.commentId,
          replayId: v.replayId
        };
        deleteCommentAndReply([params]).then(res => {
          this.$message.success("删除成功");
          this.getList();
        });
      });
    },
    batchDelete() {
      if (this.selectList.length === 0) {
        this.$message.warning("请选择要删除的数据");
      } else {
        this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          console.log(this.selectList);
          deleteCommentAndReply(this.selectList).then(res => {
            this.$message.success("删除成功");
            this.getList();
          });
        });
      }
    },
    reset() {
      this.searchForm = {
        title: "",
        memberId: "",
        verifyStatus: "",
        pageNum: 1,
        pageSize: 10,
        typeList: "1,3",
        type: ''
      };
      this.searchTime = null;
      this.getList();
    },
    search() {
      this.searchForm.pageNum = 1;
      this.$authDate(this.searchForm.beginTime, this.searchForm.endTime, v => {
        if (!v.type) {
          return this.$message.warning(v.msg);
        } else {
          // console.log(this.searchForm);
          this.getList();
        }
      });
    },
    getList() {
      this.listLoading = true;
      getArticleCommentList(this.searchForm)
        .then(res => {
          this.tableData.total = res.data.total;
          this.tableData.list = res.data.list;
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang="scss">
.feedback-content {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
